package cn.wolfcode.trip.web.controller;

import cn.wolfcode.trip.base.domain.HotelDetail;
import cn.wolfcode.trip.base.mapper.RegionMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IHotelDetailService;
import cn.wolfcode.trip.base.util.JSONResult;
import cn.wolfcode.trip.base.util.QiniuCloudUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/hotelDetail")
public class HotelDetailController {
    @Autowired
    private IHotelDetailService hotelDetailService;


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") QueryObject qo){
        qo.setPageSize(10);
        model.addAttribute("result",hotelDetailService.query(qo));

        return "hotelDetail/list";
    }



    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();

        try {
            hotelDetailService.delete(id);
        } catch (Exception e) {
            e.printStackTrace();
            result.mark("操作失败");
        }

        return result;
    }




    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(HotelDetail hotelDetail, MultipartFile img){
        if (img != null && img.getContentType().startsWith("image") && img.getSize() > 0) {
            String url = QiniuCloudUtil.uploadQiniu(img);
            hotelDetail.setCoverUrl(url);
        }

        hotelDetailService.saveOrUpdate(hotelDetail);

        return "redirect:/hotelDetail/list.do";
    }


}
